import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/index'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles), 调整为页面动态配置权限，不在需要此字段。
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/error-page/redirect.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      },
      {
        path: 'user-center',
        hidden: true,
        component: () => import('@/views/system/User.vue'),
        name: 'UserCenter',
        meta: { title: '个人中心', icon: 'user', affix: true }
      }
    ]
  },
  {
    path: '/pdd',
    component: Layout,
    name: 'Pdd',
    meta: { title: '拼多多', icon: 'shopping', affix: false },
    redirect: '/pdd/order',
    children: [
      {
        path: 'order',
        component: () => import('@/views/pdd/BatchRob.vue'),
        name: 'GoodsOrder',
        meta: { title: '备货单', icon: 'icon' }
      },
      {
        path: 'saleCount',
        component: () => import('@/views/pdd/SaleCount.vue'),
        name: 'SaleCount',
        meta: { title: '销售数据', icon: 'icon' }
      },
      {
        path: 'message',
        component: () => import('@/views/pdd/Message.vue'),
        name: 'Message',
        meta: { title: '消息通知', icon: 'icon' }
      },
      {
        path: 'inventoryManage',
        component: () => import('@/views/pdd/Inventory.vue'),
        name: 'Inventory',
        meta: { title: '库存管理', icon: 'icon' }
      },
      // {
      //   path: 'stockChart',
      //   component: () => import('@/views/pdd/StockChart.vue'),
      //   name: 'StockChart',
      //   meta: { title: '库龄报表', icon: 'icon' }
      // },
      {
        path: 'stockAccessDetail',
        component: () => import('@/views/pdd/StockAccessDetail.vue'),
        name: 'StockAccessDetail',
        meta: { title: '库存出入明细', icon: 'icon' }
      },
      {
        path: 'pddVMI',
        component: () => import('@/views/pdd/pddVMI.vue'),
        name: 'PddVMI',
        meta: { title: 'Temu库存', icon: 'icon' }
      }
    ]
  },
  {
    path: '/dataCenter',
    component: Layout,
    name: 'DataCenter',
    meta: { title: '数据中心', icon: 'shopping', affix: false },
    redirect: '/dataCenter/overview',
    children: [
      {
        path: 'overview',
        component: () => import('@/views/dataCenter/overview.vue'),
        name: 'OperateOverview',
        meta: { title: '经营概览', icon: 'icon' }
      }
      // {
      //   path: 'dashboard',
      //   component: () => import('@/views/dataCenter/dashboard.vue'),
      //   name: 'SaleDashboard',
      //   meta: { title: '销售看板', icon: 'icon' }
      // },
    ]
  },
  {
    path: '/sys',
    component: Layout,
    name: 'System',
    meta: { title: '系统管理', icon: 'lock', affix: false },
    redirect: '/sys/notice',
    children: [
      {
        path: 'notice',
        component: () => import('@/views/system/Notice.vue'),
        name: 'Notice',
        meta: { title: '系统公告', icon: 'icon' }
      },
      {
        path: 'active',
        component: () => import('@/views/system/ActiveCode.vue'),
        name: 'ActiveCode',
        meta: { title: '激活码', icon: 'icon' }
      }
    ]
  },
  {
    path: '/goods',
    component: Layout,
    name: 'Goods',
    meta: { title: '商品管理', icon: 'goods', affix: false },
    redirect: '/goods/goods',
    children: [
      {
        path: 'copy',
        name: 'copy',
        meta: { title: '商品复制', icon: 'icon' },
        component: () => import('@/views/goods/Copy.vue')
      }
    ]
  },
  // 此写法解决动态路由页面刷新的 warning 警告
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error-page/404.vue'),
    hidden: true
  }
]

export const asyncRoutes = [
  // {
  //   path: '/pdd',
  //   component: Layout,
  //   name: 'Pdd',
  //   children: [
  //     {
  //       path: 'order',
  //       component: () => import('@/views/pdd/GoodsOrder.vue'),
  //       name: 'GoodsOrder',
  //       meta: { title: '备货单', icon: 'icon' }
  //     },
  //     {
  //       path: 'saleCount',
  //       component: () => import('@/views/pdd/SaleCount.vue'),
  //       name: 'SaleCount',
  //       meta: { title: '销售数据', icon: 'icon' }
  //     }
  //   ]
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes
})

// 重置路由为静态路由
export const resetRouter = () => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && asyncRoutes.find((item) => item.name === name)) {
      router.removeRoute(name)
    }
  })
}

export default router
